﻿/*************************************************
 * 
 *  框架级
 *  
 *  数据库通用类 - 时间和时间戳的转换
 * 
 *  create by ternence
 * 
 *  create datetime at 2017-03-17
 *  
 *  西安佳卓信息科技有限公司 ©2017
 * 
 * ***********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zor_Tools
{
    public class TimeHelper
    {
        /// <summary>
        /// 时间转时间戳
        /// </summary>
        /// <param name="time">时间</param>
        /// <returns></returns>
        public static int ToTimeStamp(DateTime time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// 返回当前时间的时间戳 
        /// </summary>
        /// <returns></returns>
        public static int ToNowTimeStamp()
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (int)(DateTime.Now - startTime).TotalSeconds;
        }

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name=”timeStamp”></param>
        /// <returns></returns>
        public static DateTime TimeStampToDateTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }


        /// <summary>
        /// 返回string类型的时间格式
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <param name="formatTime"></param>
        /// <returns></returns>
        public static string TimeStampToDateTime(string timeStamp,ToTimeType formatTime)
        {
            var returnTime = "";
            switch (formatTime)
            {
                case ToTimeType.link_ymd: returnTime = TimeStampToDateTime(timeStamp).ToString("yyyy-MM-dd"); break;
                case ToTimeType.link_ymdt: returnTime = TimeStampToDateTime(timeStamp).ToString("yyyy-MM-dd HH:mm:ss"); break;
                case ToTimeType.sprit_ymd: returnTime = TimeStampToDateTime(timeStamp).ToString("yyyy/MM/dd"); break;
                case ToTimeType.sprit_ymdt: returnTime = TimeStampToDateTime(timeStamp).ToString("yyyy/MM/dd HH:mm:ss"); break;
                default: returnTime = TimeStampToDateTime(timeStamp).ToString("yyyy-MM-dd"); break;
            }
            return returnTime;
        }


        /// <summary>
        /// 返回的时间类型
        /// </summary>
        public enum ToTimeType
        {
            /// <summary>
            /// yyyy-MM-dd
            /// </summary>
            link_ymd,

            /// <summary>
            /// yyyy-MM-dd HH:mm:ss
            /// </summary>
            link_ymdt,

            /// <summary>
            /// yyyy-MM-dd
            /// </summary>
            sprit_ymd,

            /// <summary>
            /// yyyy-MM-dd HH:mm:ss
            /// </summary>
            sprit_ymdt,
        }




           

    }
}
